// pages/wattingReceipt/wattingReceipt.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        process: {
            2: "已付款",
            3: "已接单",
            4: "生产中",
            5: "运送中"
        },
        no_data_tips:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.get_storage();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    get_storage: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                var open_id = data.open_id;
                that.setData({
                    member_id: member_id,
                    open_id: open_id
                })
                that.order_list(member_id)
            }
        })
    },
    order_list: function (member_id) {
        var that = this;
        var params = {};
        params['id'] = member_id;
        params['mall_status'] = 2;
        wx.request({
            url: app.config() + "member/memberOrder",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data = res.data.data;
                var len=data.length;
                if(len){
                    for (var i = 0; i < data.length; i++) {
                        // 订单进度：1->未付款 ， 2->已付款（待接单） ， 3->已接单 ， 4->生产中 ， 5->运送中 ， 6->已签收 ， 7->退款完成
                        var process = data[i].process;
                        var express_time = parseInt(data[i].express_time);//快递时间
                        var sign_time = 86400 * 7;//自动签收天数
                        var auto_sign_time = express_time + sign_time;
                        var cur_time = (Date.parse(new Date())) / 1000;//当前时间
                        var order_id = data[i].id;
                        if (process == 5 && cur_time > auto_sign_time) {
                            var params = {};
                            params['id'] = order_id;
                            that.sign_order(params)
                        }
                        data[i].product_number = data[i].product_order.length;
                        data[i].process_cn = that.data.process[data[i].process];
                    }
                }else{
                    that.setData({
                        no_data_tips:false
                    })
                }
                
                that.setData({
                    order_list: data
                })
            }
        })
    },
    order_details: function (e) {
        var order_id = e.currentTarget.dataset.order_id;
        wx.navigateTo({
            url: '../../pages/wattingReceiptOrderInfo/wattingReceiptOrderInfo?order_id=' + order_id,
        })
    },//签收
    member_sign: function (e) {
        var that = this;
        var order_id = e.currentTarget.dataset.order_id;
        var params = {};
        params['id'] = order_id;
        that.sign_order(params)
    },
    sign_order: function (params) {
        var that=this;
        var member_id = that.data.member_id;
        wx.request({
            url: app.config() + "order/signForOrder",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    wx.showModal({
                        title: '签收成功',
                        content: '恭喜您获得' + res.data.data + '积分',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                that.order_list(member_id)
                            }
                        }
                    })
                }
            }
        })
    }
})